package com.zgj.netty_udp.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zgj.netty_udp.model.TempRecord;
import com.zgj.netty_udp.service.TempRecordService;
import com.zgj.netty_udp.utils.CRC16;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class ApiController {

    @Autowired
    private TempRecordService tempRecordService;

    @GetMapping("/tempHis")
    public Object tempHis() {
        QueryWrapper<TempRecord> q = new QueryWrapper<>();
        q.orderByDesc("create_time");
        List<TempRecord> tempRecords = tempRecordService.list(q);
        List<Map<String, String>> data = new ArrayList<>(tempRecords.size());
        tempRecords.forEach(item -> {
            Map<String, String> temMap = new HashMap<>();
            if (StrUtil.isNotEmpty(item.getCrc16())) {
                if (item.getCrc16().startsWith("8E7D")) {
                    byte[] rebyt = CRC16.toBytes(item.getCrc16());
                    temMap.put("createTime", DateUtil.format(item.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                    temMap.put("proVer", rebyt[4] + "");
                    temMap.put("manNumH", (rebyt[8] & 0xff) + "");
                    temMap.put("devId", (rebyt[10] & 0xff) + "");
                    temMap.put("seqNo", (rebyt[6] & 0xff) + "");
                    temMap.put("status", (rebyt[18] & 0xff) + "");
                    temMap.put("count", (rebyt[21] & 0xff) + "");
                    short t0 = (short) ((rebyt[23] & 0xff) | ((rebyt[24] & 0xff) << 8));
                    temMap.put("temp" + rebyt[22], (Float.parseFloat(t0 + "") / 10) + "");

                    short t1 = (short) ((rebyt[26] & 0xff) | ((rebyt[27] & 0xff) << 8));
                    temMap.put("temp" + rebyt[25], (Float.parseFloat(t1 + "") / 10) + "");

                    short t2 = (short) ((rebyt[29] & 0xff) | ((rebyt[30] & 0xff) << 8));
                    temMap.put("temp" + rebyt[28], (Float.parseFloat(t2 + "") / 10) + "");

                    short t3 = (short) ((rebyt[32] & 0xff) | ((rebyt[33] & 0xff) << 8));
                    temMap.put("temp" + rebyt[31], (Float.parseFloat(t3 + "") / 10) + "");

                    short t4 = (short) ((rebyt[35] & 0xff) | ((rebyt[36] & 0xff) << 8));
                    temMap.put("temp" + rebyt[34], (Float.parseFloat(t4 + "") / 10) + "");
                    temMap.put("data", item.getCrc16());
//                    item.setDevId((rebyt[10] & 0xff) + "");
                    data.add(temMap);
                } else {
                    temMap.put("msg", "unknown data");
                    temMap.put("data", item.getCrc16().toString());
                    data.add(temMap);
                }

            }
        });
//        if (tempRecords.size() > 0) {
//            tempRecordService.saveOrUpdateBatch(tempRecords);
//        }
        return data;
    }
}
